package com.cardbaobao.cardbabyclient.utils;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.text.Layout;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.AlignmentSpan;

import java.io.File;
import java.util.Date;

/**
 * 照片操作工具类
 *
 * @author zad
 */
public class TakePhotoUtil {

    /**
     * 拍照获得的文件名
     */
    private static String localTakeImageFileName = "cardbaobao";
    /**
     * SD卡目录
     */
    private static final File FILE_SDCARD = Environment.getExternalStorageDirectory();
    /**
     * 拍照生成的图片文件夹目录
     */
    public static final File FILE_PIC_SCREENSHOT = new File(FILE_SDCARD,
            SharedPreferencesUtil.CbbInfo.KEY_PHOTO);
    /**
     *  系统相机拍照
     */
    public static void takePhoto(Context context, int requestCode) {
        String status = Environment.getExternalStorageState();
        if (status.equals(Environment.MEDIA_MOUNTED)) {
            try {
                localTakeImageFileName = String.valueOf((new Date()).getTime()) + ".png";
                SharedPreferencesUtil.CbbInfo.setPhoto(localTakeImageFileName);//以免内存回收
                File filePath = FILE_PIC_SCREENSHOT;
                createFileScreenshots();
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                File f = new File(filePath, localTakeImageFileName);
                // localTempImgDir和localTempImageFileName是自己定义的名字
                Uri u = Uri.fromFile(f);
                intent.putExtra(MediaStore.Images.Media.ORIENTATION, 0);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, u);
                ((Activity) context).startActivityForResult(intent, requestCode);
            } catch (ActivityNotFoundException e) {
                //
            }
        }
        AlignmentSpan span = new AlignmentSpan.Standard(Layout.Alignment.ALIGN_CENTER);
        AbsoluteSizeSpan span_size = new AbsoluteSizeSpan(25, true);
        SpannableStringBuilder spannable = new SpannableStringBuilder();
        String dTitle = "请选择图片";
        spannable.append(dTitle);
        spannable.setSpan(span, 0, dTitle.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannable.setSpan(span_size, 0, dTitle.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }

    /**
     * 打开相册选择图片
     *
     * @param requestCode 请求码
     */
    public static void openAtlas(Context context, int requestCode) {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_PICK);
        intent.setType("image/*");
        ((Activity) context).startActivityForResult(intent, requestCode);
    }

    public static String getLocalTakeImageFileName() {
        if(localTakeImageFileName == null){
            localTakeImageFileName = SharedPreferencesUtil.CbbInfo.getPhoto();
        }
        return localTakeImageFileName;
    }

    public static void createFileScreenshots(){
        File filePath = FILE_PIC_SCREENSHOT;
        if (!filePath.exists()) {
            filePath.mkdirs();
        }
    }
}
